<!doctype html>
<head>
  <meta charset="utf-8">

  <title>My Bmob App</title>
  <meta name="description" content="My Bmob App">
  <meta name="viewport" content="width=device-width">
   <!-- 引入样式 -->
   <link rel="stylesheet" href="https://unpkg.com/vue-easytable/umd/css/index.css">
  <script src="https://cdn.jsdelivr.net/npm/vue"></script>
  <!-- 引入组件库 -->
	<script src="https://unpkg.com/vue-easytable/umd/js/index.js"></script>
  <script type="text/javascript" src="bmob.js"></script>
</head>

<body>
  
  <div id="main">
	<h1>开始Bmob之旅!</h1>
		<button>获取奖项数据</button>
    <div id="result1">
    		<v-table
    				:width="500"
            :columns="columns"
            :table-data="tableData"
    		></v-table>
    </div>
    <button>获取员工信息数据</button>
    <div id="result2">
    		<v-table
    				:width="500"
            :columns="columns"
            :table-data="tableData"
    		></v-table>
    </div>
  </div>

  <script type="text/javascript">

	//Bmob.initialize("Application ID", "REST API Key");
    Bmob.initialize("5c8d351182a0ff00402fc6fb93f61aff", "a84b00ca43456c628c028228ab7a3594");
	
		var jackpot = Bmob.Object.extend("jackpot");
    var query = new Bmob.Query(jackpot);
    query.equalTo("state",0);
    query.ascending("order");
    query.find({
      success: function(results) {
        // The object was retrieved successfully.
        console.log("当前有 ["+results.length+"] 个有效奖项");
        new Vue({
        	el: "#result1",
        	data: function(){
        		return {
        			tableData : JSON.parse(JSON.stringify(results)),
        			columns : [
        				{field: 'title', title:'奖项',width: 300, titleAlign: 'center',columnAlign:'center'},
                {field: 'poolsize', title: '奖池',width: 100, titleAlign: 'center',columnAlign:'center'},
                {field: 'count', title: '计数',width: 100, titleAlign: 'center',columnAlign:'center'},
        			]
        		}
        	}
        });
        /*for (var i = 0; i < results.length; i++) {
        	var object = results[i];
        	console.log(object.id + ' - ' + object.get('title')+" > 奖池数量:"+object.get("poolsize"));
        }*/
      },
      error: function(results, error) {
        alert("query object fail");
      }
    });
    // 查询所有用户
    var users = Bmob.Object.extend("users");
    var userQuery = new Bmob.Query(users);
    //userQuery.equalTo("state",0);
    userQuery.find({
      success: function(results) {
        // The object was retrieved successfully.
        console.log("当前有 ["+results.length+"] 个有效用户");
        new Vue({
        	el: "#result2",
        	data: function(){
        		return {
        			tableData : JSON.parse(JSON.stringify(results)),
        			columns : [
        				{field: 'code', title: '编号', width: 100, titleAlign: 'center',columnAlign:'center'},
        				{field: 'name', title:'姓名', width: 300, titleAlign: 'center',columnAlign:'center'},
                {field: 'rate', title: '概率', width: 100, titleAlign: 'center',columnAlign:'center'}
        			]
        		}
        	}
        });
      },
      error: function(results, error) {
        alert("query object fail");
      }
    });
		/*var TestObject = Bmob.Object.extend("TestObject");
    var testObject = new TestObject();
      testObject.save({foo: "bar"}, {
      success: function(object) {
        $(".success").show();
      },
      error: function(model, error) {
        $(".error").show();
      }
    });*/
	
	/*
	//获取一行数据
	var GameScore = Bmob.Object.extend("GameScore");
    var query = new Bmob.Query(GameScore);
    query.get("4edc3f6ee9", {
      success: function(object) {
        // The object was retrieved successfully.
        alert(object.get("score"));
      },
      error: function(object, error) {
        alert("query object fail");
      }
    });
	*/
	
	/*
	//修改一行数据
	var GameScore = Bmob.Object.extend("GameScore");
    var query = new Bmob.Query(GameScore);
    query.get("4edc3f6ee9", {
      success: function(object) {
        // The object was retrieved successfully.
        object.set("score", 1338);
        object.save(null, {
          success: function(objectUpdate) {
            alert("create object success, object score:"+objectUpdate.get("score"));
          },
          error: function(model, error) {
            alert("create object fail");
          }
        });
      },
      error: function(object, error) {
        alert("query object fail");
      }
    });
	*/

	/*	
	//删除一行数据
	var GameScore = Bmob.Object.extend("GameScore");
    var query = new Bmob.Query(GameScore);
    query.get("4edc3f6ee9", {
      success: function(object) {
        // The object was retrieved successfully.
        object.destroy({
          success: function(deleteObject) {
          alert("delete success");
          },
          error: function(GameScoretest, error) {
          alert("delete fail");
          }
        });
      },
      error: function(object, error) {
        alert("query object fail");
      }
    });
	*/

	
  </script>
</body>

</html>
